<template>
  <div class="box" style="margin-top:30px;">
    <div class="item1">
      <ImportModal ref="importModalId" @importData="onImportData" />
    </div>
    <a-button class="addBtn" type="primary" @click="onAdd">新增</a-button>
    <img class="img" alt="Vue logo" style="width: 24px; height: 24px" src="./img/icon.png" />
    <CustomTable
      class="item2"
      ref="table"
      :showAlert="false"
      :columns="columns"
      :dataSource="pageData"
      :showSelection="false"
      :loading="loading"
      :showPagination="true"
      rowKey="id"
      @change="onTableChange"
    >
      <template slot="order" slot-scope="text, record, index">
        {{ index + 1 }}
      </template>
      <template slot="phoneNumber" slot-scope="text, record, index">
        <a-input
          v-if="isEdit[getIndex(index)]"
          v-model="recordData[getIndex(index)].phoneNumber"
          placeholder="请输入"
        />
        <span v-else
          >{{ recordData[getIndex(index)].phoneNumber }}
          <a-tag v-if="isFyb[getIndex(index)]" style="margin-left:20px;" color="red">负样本人员</a-tag></span
        >
      </template>
      <template slot="userName" slot-scope="text, record, index">
        <a-input v-if="isEdit[getIndex(index)]" v-model="recordData[getIndex(index)].userName" placeholder="请输入" />
        <span v-else>{{ recordData[getIndex(index)].userName }}</span>
      </template>
      <template slot="action" slot-scope="text, record, index">
        <a-space size="large">
          <!-- {{ getIndex(index) }}
          {{ record.id }}
          {{ pageData.pages }} -->
          <a v-if="isEdit[getIndex(index)]" @click="onConfirm(getIndex(index))">保存</a>
          <a v-else @click="onEdit(getIndex(index))">编辑</a>
          <a-popconfirm title="是否要删除记录吗?" ok-text="是" cancel-text="否" @confirm="onDelete(getIndex(index))">
            <a>删除</a>
          </a-popconfirm>
        </a-space>
      </template>
    </CustomTable>
  </div>
</template>

<script>
import CustomTable from '@/components/zxd/Table/CustomTable'
import ImportModal from './ImportModal'
import * as caseApi from '@/api/wxSmuggle/caseEvent.js'
export default {
  components: { CustomTable, ImportModal },
  data() {
    return {
      pageData: {},
      columns: [
        {
          title: '手机',
          dataIndex: 'phoneNumber',
          align: 'left',
          scopedSlots: { customRender: 'phoneNumber' }
        },
        {
          title: '姓名',
          dataIndex: 'userName',
          align: 'left',
          scopedSlots: { customRender: 'userName' }
        },
        {
          title: '操作',
          key: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' },
          width: 200
        }
      ],
      isEdit: [],
      isFyb: [],
      recordData: [],
      loading: false
    }
  },

  created() {},
  methods: {
    onClearRowKeys() {
      this.$refs.table.onClearRowKeys()
    },
    getIndex(index) {
      return (this.pageData.current - 1) * this.pageData.size + index
    },
    onAdd() {
      // console.log(this.recordData.length)
      //插入第一个
      if (this.recordData.length > 1000) {
        this.$message.warning('数量不能超过1000条！')
        return
      }
      this.recordData.unshift({ phoneNumber: '', userName: '' })
      this.isEdit.unshift(true)
      this.isFyb.unshift(false)
      //页面跳到第一页
      this.refreshTable(1)
    },
    onImportData(arr) {
      if (arr.length > 1000) {
        this.$message.warning('数量不能超过1000条！')
        return
      }
      arr.forEach((item, index) => {
        this.recordData.push({ phoneNumber: item.phoneNumber, userName: item.userName })
        this.isEdit.push(false)
        //判断是否是负样本人员
        this.$set(this.isFyb, index, item.isSamplePerson)
      })
      this.loading = true
      this.refreshTable(1)
    },
    refreshTable(current) {
      this.loading = false
      this.pageData = {
        current: current,
        pages: parseInt((this.recordData.length - 1) / 6) + 1,
        size: 6,
        records: this.recordData,
        total: this.recordData.length
      }
    },
    onEdit(index) {
      this.$set(this.isEdit, index, true)
    },
    onDelete(index) {
      this.recordData.splice(index, 1)
      this.isEdit.splice(index, 1)
      //页面跳到第一页
      this.loading = true
      setTimeout(() => {
        this.refreshTable(1)
      }, 1000)
    },
    onConfirm(index) {
      // console.log('11111111111111')
      // console.log(index, this.recordData[index].phoneNumber)
      if (this.recordData[index].phoneNumber == '' || this.recordData[index].phoneNumber == undefined) {
        this.$message.warning('手机号码不能为空！')
        return
      }
      let flag = this.phoneValidata(this.recordData[index].phoneNumber)
      if (flag) {
        this.$message.warning('手机格式不正确！')
        return
      }
      if (this.recordData[index].userName == '') {
        this.$message.warning('姓名不能为空！')
        return
      }
      this.$set(this.isEdit, index, false)
      //请求负样本接口，确定是否是负样本号码
      caseApi
        .getSampleByPhone({ phone: this.recordData[index].phoneNumber })
        .then(res => {
          if (res.code == 200) {
            this.$set(this.isFyb, index, res.result)
          }
        })
        .catch(() => {
          this.$message.error(res.message)
        })
    },
    onTableChange(pagination, pageFilter, sorter) {
      this.refreshTable(pagination.currentPage)
    },
    /**
  手机号码检验
 */
    phoneValidata(value) {
      // let reg = new RegExp(/^1[3|4|5|6|7|8|9][0-9]{9}$/)
      // let reg = new RegExp(/^((\+?86)|(\(\+86\)))?\d{3,4}-\d{7,8}(-\d{3,4})?$/)
      // let reg = new RegExp(/^((\+?86)|(\(\+86\)))?1\d{10}$/)
      let flag = false
      let reg = new RegExp(/^((\+?86)|(\(\+86\)))?1[3456789]\d{9}$/) // ^((\\+86)|(86))?[1][3456789][0-9]{9}$
      if (value && !reg.test(value)) {
        flag = true
      }
      return flag
    },
    onValidate() {
      let length = this.recordData.length
      let flag = true
      if (length <= 0) {
        this.$message.warning('请添加或者导入目标人')
        flag = false
      }
      if (flag) {
        this.isEdit.forEach(item => {
          // console.log('item')
          // console.log(item)
          if (item) {
            this.$message.warning('请保存目标人')
            flag = false
          }
        })
      }
      return flag
    },
    onValidate1() {
      let flag = true
      this.isEdit.forEach(item => {
        // console.log('item')
        // console.log(item)
        if (item) {
          this.$message.warning('请保存目标人')
          flag = false
        }
      })
      return flag
    },
    getData() {
      return this.recordData
    },
    setData(arr) {
      this.recordData = arr
      this.refreshTable(1)
    }
  }
}
</script>
<style lang="less" scoped>
/deep/.ant-pagination-options-size-changer.ant-select {
  display: none;
}
.box {
  position: relative;
  overflow: hidden;
  height: 500px;
  .item1 {
    float: left;
    width: 35%;
    height: 100%;
  }
  .item2 {
    float: left;
    width: 55%;
    height: 100%;
    margin-left: 10%;
  }
  .addBtn {
    position: absolute;
    left: 37%;
    top: 0;
  }
  .img {
    position: absolute;
    left: 39%;
    top: 45%;
  }
  /deep/.ant-table-wrapper {
    height: 100%;
  }
  /deep/.ant-table-placeholder {
    border-bottom: none;
  }
}
</style>
